package example.documentbased.documentModule.controller
{
	import example.documentbased.mainModule.models.doman.Document;
	import example.documentbased.mainModule.models.mappers.DocumentMapper;
	import example.documentbased.mainModule.models.vo.DocumentVO;
	import example.documentbased.mainModule.services.IDocumentService;
	
	import org.etherframework.controller.IResponderCommand;
	import org.etherframework.controller.ResponderCommand;
	
	import raix.reactive.IObservable;
	import raix.reactive.Observable;
	import raix.reactive.flex.IObservableResponder;
	
	public class GetDocumentContentCommand extends ResponderCommand
	{
		private var _documentService:IDocumentService;
		private var _document:DocumentVO;
		private var _documentMapper:DocumentMapper;
		
		public function GetDocumentContentCommand(documentService:IDocumentService, 
												  documentMapper:DocumentMapper)
		{
			_documentService = documentService;
			_documentMapper = documentMapper;
		}
		
		public function initialize(document:DocumentVO):IResponderCommand
		{
			_document = document;
			markAsInitialized();
			
			return this;
		}
		
		override public function observable():IObservable
		{
			var document:Document = _documentMapper.toDomain(_document);
			return processing(_documentService.getDocumentContent(document)
				.mapMany(function(value:*):IObservable
				{
					return result(value);
				}));
		}
	}
}